#include "response.h"
#include "utils.h"

using namespace bhttp;

std::vector<char>& response::headers()
{
    return headers_;
}

std::string response::header(const std::string& key)
{
    std::vector<std::string> hds = utils::split(std::string(headers_.data(), headers_.size()), "\r\n");
    std::string k, v;
    for (const auto& hd : hds)
    {
        if (hd.find(":") == std::string::npos)
            continue;
        k.clear();
        v.clear();
        utils::split(hd, ":", &k, &v);
        if (utils::strip(key) == k)
            return v;
    }
    return std::string();
}

std::vector<char>& response::body()
{
    return body_;
}

std::string response::body_string() const
{
    if (body_.empty()) return std::string();
    return std::string(body_.data(), body_.size());
}

long& response::code()
{
    return code_;
}

double& response::total_msec()
{
    return total_msec_;
}

void response::successed(const bool succ)
{
    successed_ = succ;
}

bool response::successed() const
{
    return successed_;
}

char* response::error()
{
    return error_;
}
